<?php

namespace App\Http\Controllers\Resources;

use App\Models\SimCard;
use App\Services\HelpService;
use App\Services\Interfaces\RepoServiceInterface;
use Illuminate\Http\Request;
use App\Http\Requests;
use \App\Http\Requests\SimCardRequest;
use App\Http\Controllers\Controller;
use Helper;

class SimCardController extends Controller
{
    public function __construct(RepoServiceInterface $_repoService)
    {
        $this->_repoService = $_repoService;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($indexName = null)
    {
        $option = 'List';
        if (is_null($indexName))  {
            $result = $this->_repoService->read();
            return $result;
        } else {
            $data = HelpService::get_filter();
            switch ($indexName) {
                case 'books':
                    $lists = SimCard::$option($data)
                        ->distinct('index.book');
                    break;
                case 'pages':
                    $lists = SimCard::$option($data)
                        ->distinct('index.page');
                    break;
                case 'boxes':
                    $lists = SimCard::$option($data)
                        ->distinct('index.box');
                    break;
                case 'box':
                    $lists = SimCard::$option($data)
                        ->first();
                    return $lists;
                default:
                    return $this->_error('参数不合法');
            }
            $lists = $lists->get()->toArray();
            $lists = array_map(function ($list) {
                return $list[0];
            }, $lists);
            sort($lists);
            return ['totle' => count($lists),
                'data'=>$lists];
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(SimCardRequest $request)
    {
        $data = $request->all();
        $count = $this->_repoService->createMany($data);
        if ($count) {
            $result['total'] = $count;
            return $result;
        } else {
            return $this->_error('没有创建新卡柜');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $sim_card = SimCard::find($id);
        if (is_null($sim_card)) {
            return $this->_error('该卡柜不存在');
        }
        return $sim_card;
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(SimCardRequest $request, $id)
    {
        $sim_card = SimCard::find($id);
        $data = $request->all();
        if (is_null($sim_card)) {
            return $this->_error('该卡柜不存在');
        }
        if (!empty($sim_card->mobile)) {
            return $this->_error('该卡柜已被占用');
        }
        $filter = ['id' => $id];
        $isSave = $this->_repoService->update($filter, $data);
        if($isSave)
            return SimCard::find($id);
        else
            return $this->_error('修改失败');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function import(Request $request) {
        //判断文件是否存在
        if (!$request->hasFile('excel')) {
            return $this->_error('未上传表格');
        }
        $excel = $request->file('excel');
        $upload_name = Helper::transcoding($excel->getClientOriginalName(), false);
        //拓展名判断
        if (substr($upload_name, -5, 5) !== '.xlsx') {
            return $this->_error('上传文件格式错误');
        }
        $file_name = Helper::fileName(storage_path('mobile/'), 1, '', 'xlsx');
        if ($excel->isValid()) {
            $excel->move(storage_path('mobile'), $file_name);
        } else {
            return $this->_error('上传失败,请稍后再试');
        }
        //数据处理
        return $this->_repoService
            ->importMobile(storage_path('mobile/' . $file_name));
    }

    public function export() {
        $path = storage_path('mobile/example/example.xlsx');
        HelpService::exportModel($path);
    }
}
